package thread.mianshi;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author wangzepeng
 * @data  2018-03-08
 */
public class CallableImpl implements Callable<Map<String,Integer>> {

    /**
     * cas 原子类
     */
    static AtomicInteger count = new AtomicInteger(0);


    static AtomicInteger failCount = new AtomicInteger(0);

    private List<List<Integer>> dataList = null;

    public void setList(List<List<Integer>> list){
        this.dataList = list;
    }

    @Override
    public Map<String,Integer> call()  {
        try {
            System.out.printf(":%s开始执行方法,打印每次处理的数据：%s\n",Thread.currentThread().getName(),Arrays.toString(dataList.toArray()));
            for(List<Integer> integers :dataList){
                for(Integer integer:integers){
                    if(integer.intValue()%2==0){
                        //成功
                        count.incrementAndGet();
                    }else{
                        boolean flag = false;
                        //失败 重新处理 累加数据
                        for(int i = 0;i<3;i++){
                            if (integer.intValue()%2==0){
                                count.incrementAndGet();
                                flag = true;
                                break;
                            }
                        }
                        if (flag == false) {
                            failCount.incrementAndGet();
                        }

                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            HashMap<String,Integer> map = new HashMap<>();
            map.put("success",count.intValue());
            map.put("fail",failCount.intValue());
           return map;
        }
    }
}
